package com.hydra.wiki.web.init;

import com.hydra.wiki.parameter.service.impl.ConstantVarService;
import com.hydra.wiki.parameter.service.impl.PropertiesFileService;
import com.hydra.wiki.web.constant.FarmConstant;
import com.hydra.wiki.web.task.ServletInitJobInter;
import org.springframework.stereotype.Component;

import javax.servlet.ServletContext;
import java.text.DecimalFormat;

@Component
public class InitParameter implements ServletInitJobInter {

    private static void memory() {
        DecimalFormat df = new DecimalFormat("0.00");
        // Display the total amount of memory in the Java virtual machine.
        long totalMem = Runtime.getRuntime().totalMemory();
        ConstantVarService.registConstant("farm.jvm.memory.total", df.format(totalMem / 1024 / 1024) + " MB");
        // Display the maximum amount of memory that the Java virtual machine
        // will attempt to use.
        long maxMem = Runtime.getRuntime().maxMemory();
        ConstantVarService.registConstant("farm.jvm.memory.max", df.format(maxMem / 1024 / 1024) + " MB");
        // Display the amount of free memory in the Java Virtual Machine.
        long freeMem = Runtime.getRuntime().freeMemory();
        ConstantVarService.registConstant("farm.jvm.memory.free", df.format(freeMem / 1024 / 1024) + " MB");
    }

    @Override
    public void execute(ServletContext context) {
        // 注册常量
        ConstantVarService.registConstant("farm.constant.session.key.logintime.", FarmConstant.SESSION_LOGINTIME);
        ConstantVarService.registConstant("farm.constant.session.key.current.org", FarmConstant.SESSION_ORG);
        ConstantVarService.registConstant("farm.constant.session.key.current.roles", FarmConstant.SESSION_ROLES);
        ConstantVarService.registConstant("farm.constant.session.key.current.actions", FarmConstant.SESSION_USERACTION);
        ConstantVarService.registConstant("farm.constant.session.key.current.menu", FarmConstant.SESSION_USERMENU);
        ConstantVarService.registConstant("farm.constant.session.key.current.user", FarmConstant.SESSION_USEROBJ);
        ConstantVarService.registConstant("farm.constant.session.key.current.userphoto",
                FarmConstant.SESSION_USERPHOTO);
        ConstantVarService.registConstant("farm.constant.session.key.go.url", FarmConstant.SESSION_GO_URL);
        ConstantVarService.registConstant("farm.constant.session.key.from.url", FarmConstant.SESSION_FROM_URL);
        ConstantVarService.registConstant("farm.constant.app.treecodelen",
                String.valueOf(FarmConstant.MENU_TREECODE_UNIT_LENGTH));
        //TODO 修正位置
        ConstantVarService.registConstant("farm.constant.webroot.path", context.getRealPath(""));
        // 注册配置文件
        PropertiesFileService.registConstant("config");
        PropertiesFileService.registConstant("wda");
        PropertiesFileService.registConstant("indexConfig");
        PropertiesFileService.registConstant("document");
        PropertiesFileService.registConstant("cache");
        PropertiesFileService.registConstant("webapp");
        PropertiesFileService.registConstant("i18n");
        PropertiesFileService.registConstant("about");
        PropertiesFileService.registConstant("rmi");
        PropertiesFileService.registConstant("email");
        memory();
    }
}
